import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter,
  ElementRef,
  AfterViewInit,
  ViewEncapsulation,
  Inject,
  forwardRef,
} from "@angular/core";
import { ModalDirective } from "ngx-bootstrap";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineListDto,
  SimpleTechDocListDto,
  TechDocDto,
  TechDocServiceProxy,
  RawMaterialDto,
  EquipmentInfoDto,
  ProcInfoDto,
  ComboboxItemDto,
  TechDocInput,
} from "@shared/service-proxies/service-proxies";
import { PipelineNodeServiceProxy } from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { ActBaseModalComponent } from "@app/pipeline/shared/act-base.component";

@Component({
  selector: "gy06",
  templateUrl: "./gy06.component.html",
  styleUrls: ["./gy06.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class GY06ModalComponent extends AppComponentBase {
  saving = false;
  isLoading = false;
  pipe: PipelineListDto = new PipelineListDto();

  techDocs = new Array<SimpleTechDocListDto>();
  selectedTechDoc = new SimpleTechDocListDto();
  filterText = "";
  material = new Array<RawMaterialDto>();
  equips = new Array<EquipmentInfoDto>();
  procs = new Array<ProcInfoDto>();
  equipments = new Array<ComboboxItemDto>();
  boundedTechDoc = new SimpleTechDocListDto();
  isSelected=1;

  constructor(
    injector: Injector,
    private _techDocServiceProxy: TechDocServiceProxy,
    private _taskServiceProxy: PipelineNodeServiceProxy //@Inject(forwardRef(() => ActBaseModalComponent)) actBase: ActBaseModalComponent
  ) {
    super(injector);
    //this.actBase = actBase;
  }

  load(pipe: PipelineListDto): void {
    this.primengTableHelper.showLoadingIndicator();
    this.pipe = pipe;
    this.filterText = pipe.drawingCode;
    this.getTechDoc();
  }
  getTechDoc() {
    //this._taskServiceProxy.getTechDocList(pipe.id).subscribe(result => {
    this._taskServiceProxy
      .getTechDocListForGY(this.pipe.id, this.filterText)
      .subscribe(result => {
        this.techDocs = result.techDocs;
        this.equipments = result.equipments;
        this.selectedTechDoc = result.techDocs.filter(
          el => el.codeId === result.selectedCodeId
        )[0];
        this.boundedTechDoc = result.techDocs.filter(
          el => el.codeId === result.boundedCodeId
        )[0];

        this.getTechDocInfo(result.selectedCodeId);
      });
  }

  private getTechDocInfo(codeId: string) {
    this.isLoading = true;
    this._taskServiceProxy
      .getTechDocInfo(this.pipe.id, codeId)
      .pipe(
        finalize(() => {
          this.saving = false;
          this.isLoading = false;
        })
      )
      .subscribe(result => {
        this.material =result.rawMaterialInfos;
          // result.rawMaterialInfos !== null &&
          // result.rawMaterialInfos.length !== 0
          //   ? result.rawMaterialInfos[0]
          //   : new RawMaterialDto();
        this.equips = result.equipmentInfos;
        this.procs = result.procInfos;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  rowSelect() {
    this.primengTableHelper.showLoadingIndicator();
    let rowData = this.selectedTechDoc;
    this.getTechDocInfo(rowData.codeId);
  }

  save(): void {
    this.saving = true;
    let input = new TechDocInput();
    // input.paiHao = this.material.newPaiHao;
    // input.materialStatus = this.material.newMaterialStatus;
    // input.paiHaoStandard = this.material.newPaiHaoStandard;
    input.pipeId = this.pipe.id;
    input.techDocCode = this.selectedTechDoc.codeId;
    input.equipmentInfos = this.equips;
    input.procInfos=this.procs;
    input.rawMaterialInfos=this.material;
    input.index=this.isSelected;
    this._taskServiceProxy
      .againBoundTechDoc(
        input
      )
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe((result) => {
        this.notify.success("绑定成功");
      });
  }
  onChange(sequenceNumber: number) {
    // console.log(sequenceNumber);
    this.isSelected = sequenceNumber;
  }
}
